/****************************************************************************
**
****************************************************************************/

#include "recordMain.h"

#include "../oiovMain.h"
#include "../oiovCallback.h"

#include "../../iov/operation/operation_protocol.h"

#include <QDebug>
//#define ORDERRECORD_DEBUG

BaseRecord RecordMain::m_baseRecord;

RecordMain::RecordMain(QObject *parent) : QThread(parent),
    m_quit(false)
{
    /*
     * idx - 主键/自动增长/非空
     * proto_type - 协议类型
     * cp_type - 充电桩类型
     * pile_num - 桩编号
     * gnu_no -枪号
     * trade_no - 流水号
     * start_type - 启动类型
     * start_time - 充电开始时间
     * stop_time - 充电结束时间
     * stop_reason - 停止原因
     * charging_time - 充电时长
     * vin_code - VIN 码
     * card_code - 卡号
     * sharp_elec_unit_fee - 尖价格（电费价格）【单价】
     * sharp_service_unit_fee - 尖价格（服务费价格）【单价】
     * sharp_total_elect - 尖电量
     * sharp_loss_elect - 尖计损电量
     * sharp_elec_total_fee - 尖费用（电费费用）【费用】
     * sharp_service_total_fee - 尖费用（服务费费用）【费用】
     * peak_elec_unit_fee - 峰价格（电费价格）【单价】
     * peak_service_unit_fee - 峰价格（服务费价格）【单价】
     * peak_total_elect - 峰电量
     * peak_loss_elect - 峰计损电量
     * peak_elec_total_fee - 峰费用（电费费用）【费用】
     * peak_service_total_fee - 峰费用（服务费费用）【费用】
     * flat_elec_unit_fee - 平价格（电费价格）【单价】
     * flat_service_unit_fee - 平价格（服务费价格）【单价】
     * flat_total_elect - 平电量
     * flat_loss_elect - 平计损电量
     * flat_elec_total_fee - 平费用（电费费用）【费用】
     * flat_service_total_fee - 平费用（服务费费用）【费用】
     * valley_elec_unit_fee - 谷价格（电费价格）【单价】
     * valley_service_unit_fee - 谷价格（服务费价格）【单价】
     * valley_total_elect - 谷电量
     * valley_loss_elect - 谷计损电量
     * valley_elec_total_fee - 谷费用（电费费用）【费用】
     * valley_service_total_fee - 谷费用（服务费费用）【费用】
     * total_meter_start - 电表总起值
     * total_meter_stop - 电表总止值
     * total_elect - 充电电量
     * total_loss_elect - 充电计损电量
     * total_elec_cost - 充电费用（电费费用）
     * total_service_cost - 充电费用（服务费费用）
     * order_state - 订单状态
     */

    m_table_field =
        "proto_type INTEGER, "
        "cp_type INTEGER, "
        "pile_num TEXT, "
        "gnu_no INTEGER, "
        "trade_no TEXT, "
        "start_type INTEGER, "
        "start_time TEXT, "
        "stop_time TEXT, "
        "stop_reason INTEGER, "
        "charging_time INTEGER, "
        "vin_code TEXT, "
        "card_code TEXT, "
        "sharp_elec_unit_fee INTEGER, "
        "sharp_service_unit_fee INTEGER, "
        "sharp_total_elect INTEGER, "
        "sharp_loss_elect INTEGER, "
        "sharp_elec_total_fee INTEGER, "
        "sharp_service_total_fee INTEGER, "
        "peak_elec_unit_fee INTEGER, "
        "peak_service_unit_fee INTEGER, "
        "peak_total_elect INTEGER, "
        "peak_loss_elect INTEGER, "
        "peak_elec_total_fee INTEGER, "
        "peak_service_total_fee INTEGER, "
        "flat_elec_unit_fee INTEGER, "
        "flat_service_unit_fee INTEGER, "
        "flat_total_elect INTEGER, "
        "flat_loss_elect INTEGER, "
        "flat_elec_total_fee INTEGER, "
        "flat_service_total_fee INTEGER, "
        "valley_elec_unit_fee INTEGER, "
        "valley_service_unit_fee INTEGER, "
        "valley_total_elect INTEGER, "
        "valley_loss_elect INTEGER, "
        "valley_elec_total_fee INTEGER, "
        "valley_service_total_fee INTEGER, "
        "total_meter_start TEXT, "
        "total_meter_stop TEXT, "
        "total_elect INTEGER, "
        "total_loss_elect INTEGER, "
        "total_elec_cost INTEGER, "
        "total_service_cost INTEGER, "
        "order_state INTEGER";

    //m_baseRecord.InitRecorDB("order.db");
    //m_baseRecord.CreateRecordTable("order_table", m_table_field);
}

RecordMain::~RecordMain()
{
    m_quit = true;
    wait();
}

void RecordMain::run()
{
    m_quit = false;

    QByteArray barray;
    QVariantHash vhash;

    barray.resize(64);

    while (1) {
        //...

        if (!m_baseRecord.IsRecordExist("order_state", RecordMain::KOrderPending)) {
            msleep(1 * 3000);
            continue;
        }
        msleep(1 * 5000);
        if (!m_baseRecord.IsRecordExist("order_state", RecordMain::KOrderPending)) {
            msleep(1 * 3000);
            continue;
        }

        vhash.clear();
        vhash = m_baseRecord.RecordExistData("order_state", RecordMain::KOrderPending);
        if (vhash.isEmpty()) {
            msleep(1 * 3000);
            continue;
        }

#ifdef ORDERRECORD_DEBUG
        qDebug() << "[ORDER]-> trade is" << vhash.value("trade_no").toString();
#endif

        barray.clear();
        barray = vhash.value("trade_no").toByteArray();
        memset((void *) & (OiovCallback::ops_event_rebill.serial_number), 0x00, sizeof(OiovCallback::ops_event_rebill.serial_number));
        memcpy((void *) & (OiovCallback::ops_event_rebill.serial_number), (const void *)(barray.data()), sizeof(OiovCallback::ops_event_rebill.serial_number));
        barray.clear();
        barray = vhash.value("pile_num").toByteArray();
        memset((void *) & (OiovCallback::ops_event_rebill.pill_number), 0x00, sizeof(OiovCallback::ops_event_rebill.pill_number));
        memcpy((void *) & (OiovCallback::ops_event_rebill.pill_number), (const void *)(barray.data()), sizeof(OiovCallback::ops_event_rebill.pill_number));
        OiovCallback::ops_event_rebill.gun_number = vhash.value("pile_num").toInt();
        barray.clear();
        barray = vhash.value("start_time").toByteArray();
        memcpy((void *) & (OiovCallback::ops_event_rebill.start_time.tm), (const void *)(barray.data()), sizeof(OiovCallback::ops_event_rebill.start_time.tm));
        barray.clear();
        barray = vhash.value("stop_time").toByteArray();
        memset((void *) & (OiovCallback::ops_event_rebill.stop_time.tm), 0x00, sizeof(OiovCallback::ops_event_rebill.stop_time.tm));
        memcpy((void *) & (OiovCallback::ops_event_rebill.stop_time.tm), (const void *)(barray.data()), sizeof(OiovCallback::ops_event_rebill.stop_time.tm));
        OiovCallback::ops_event_rebill.sharp_unit_fee = vhash.value("sharp_elec_unit_fee").toInt() + vhash.value("sharp_elec_unit_fee").toInt();
        OiovCallback::ops_event_rebill.sharp_total_elect = vhash.value("sharp_total_elect").toInt();
        OiovCallback::ops_event_rebill.sharp_loss_calculation_elect = vhash.value("sharp_loss_elect").toInt();
        OiovCallback::ops_event_rebill.sharp_total_fee = vhash.value("sharp_elec_total_fee").toInt() + vhash.value("sharp_service_total_fee").toInt();
        OiovCallback::ops_event_rebill.peak_unit_fee = vhash.value("peak_elec_unit_fee").toInt() + vhash.value("peak_service_unit_fee").toInt();
        OiovCallback::ops_event_rebill.peak_total_elect = vhash.value("peak_total_elect").toInt();
        OiovCallback::ops_event_rebill.peak_loss_calculation_elect = vhash.value("peak_loss_elect").toInt();
        OiovCallback::ops_event_rebill.peak_total_fee = vhash.value("peak_elec_total_fee").toInt() + vhash.value("peak_service_total_fee").toInt();
        OiovCallback::ops_event_rebill.flat_unit_fee = vhash.value("flat_elec_unit_fee").toInt() + vhash.value("flat_service_unit_fee").toInt();
        OiovCallback::ops_event_rebill.flat_total_elect = vhash.value("flat_total_elect").toInt();
        OiovCallback::ops_event_rebill.flat_loss_calculation_elect = vhash.value("flat_loss_elect").toInt();
        OiovCallback::ops_event_rebill.flat_total_fee = vhash.value("flat_elec_total_fee").toInt() + vhash.value("flat_service_total_fee").toInt();
        OiovCallback::ops_event_rebill.valley_unit_fee = vhash.value("valley_elec_unit_fee").toInt() + vhash.value("valley_service_unit_fee").toInt();
        OiovCallback::ops_event_rebill.valley_total_elect = vhash.value("valley_total_elect").toInt();
        OiovCallback::ops_event_rebill.valley_loss_calculation_elect = vhash.value("valley_loss_elect").toInt();
        OiovCallback::ops_event_rebill.valley_total_fee = vhash.value("valley_elec_total_fee").toInt() + vhash.value("valley_service_total_fee").toInt();
        memset(OiovCallback::ops_event_rebill.total_meter_start, 0x00, sizeof(OiovCallback::ops_event_rebill.total_meter_start));
        memset(OiovCallback::ops_event_rebill.total_meter_stop, 0x00, sizeof(OiovCallback::ops_event_rebill.total_meter_stop));
        OiovCallback::ops_event_rebill.total_elect = vhash.value("total_elect").toInt();
        OiovCallback::ops_event_rebill.total_loss_calculation_elect = vhash.value("total_loss_elect").toInt();
        OiovCallback::ops_event_rebill.charging_fee = vhash.value("total_elec_cost").toInt() + vhash.value("total_service_cost").toInt();
        barray.clear();
        barray = vhash.value("vin_code").toByteArray();
        memset((void *) & (OiovCallback::ops_event_rebill.vin), 0x00, sizeof(OiovCallback::ops_event_rebill.vin));
        memcpy((void *) & (OiovCallback::ops_event_rebill.vin), (const void *)(barray.data()), sizeof(OiovCallback::ops_event_rebill.vin));
        OiovCallback::ops_event_rebill.transaction_type = vhash.value("start_type").toInt();
        memset((void *) & (OiovCallback::ops_event_rebill.transaction_date.tm), 0x00, sizeof(OiovCallback::ops_event_rebill.transaction_date.tm));
        memcpy((void *) & (OiovCallback::ops_event_rebill.transaction_date.tm), (void *) & (OiovCallback::ops_event_rebill.start_time.tm), 7);
        OiovCallback::ops_event_rebill.stop_reason = vhash.value("stop_reason").toInt();
        barray.clear();
        barray = vhash.value("card_code").toByteArray();
        memset((void *) & (OiovCallback::ops_event_rebill.physical_card_number), 0x00, sizeof(OiovCallback::ops_event_rebill.physical_card_number));
        memcpy((void *) & (OiovCallback::ops_event_rebill.physical_card_number), (const void *)(barray.data()), sizeof(OiovCallback::ops_event_rebill.physical_card_number));

        OiovReqMain::SetRebillChargeEvent();
    }

    qDebug() << "[ORDER]-> order quit";
}
